﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiveManager
{
    public partial class ProductGroups : Form
    {
        protected DiveDatabaseEntities diveDatabase;
        protected BindingSource bindingSource1;
        protected BindingSource bindingSource2;


        public ProductGroups()
        {
            InitializeComponent();

            // Init Database
            diveDatabase = new DiveDatabaseEntities();
            bindingSource1 = new BindingSource();
            bindingSource2 = new BindingSource();
        }

        private void PorductGroups_Load(object sender, EventArgs e)
        {
            // Load list
            LoadData();

            


            // Add Bindings
            txID.DataBindings.Add(new Binding("Text", bindingSource1, "ID"));

            
            
        }

        protected void LoadData()
        {

            IQueryable<ProductHeadGroups> dataQuery = (from phg in diveDatabase.ProductHeadGroups
                                                   select phg);

            // Bind Data to BindingSource
            bindingSource1.DataSource = dataQuery;

            // Bind BindingSource to List
            HeadGroupList.DataSource = bindingSource1;


            // Hide ID-Columns
            HeadGroupList.Columns["ID"].Visible = false;
            HeadGroupList.Columns["Groupname"].Visible = true;
            

            // Set Column Headers
            HeadGroupList.Columns["Groupname"].HeaderText = "Hauptgruppe";
            
        }

        protected void LoadData2()
        {
            // Add Bindings
            //txID.DataBindings.Add(new Binding("Text", bindingSource, "ID"));
            
            Guid id_guid = new Guid(txID.Text);
            
            IQueryable<ProductSubGroups> dataQuery2 = (from psg in diveDatabase.ProductSubGroups
                                                      where psg.Head_Group == id_guid
                                                      select psg);

            // Bind Data to BindingSource
            bindingSource2.DataSource = dataQuery2;

            // Bind BindingSource to List
            SubGroupList.DataSource = bindingSource2;


            // Hide ID-Columns
            SubGroupList.Columns["ID"].Visible = false;
            SubGroupList.Columns["Head_Group"].Visible = false;
            SubGroupList.Columns["Sub_Group"].Visible = true;


            // Set Column Headers
            SubGroupList.Columns["Sub_Group"].HeaderText = "Untergruppe";

        }

       
        private void button1_Click(object sender, EventArgs e)
        {
            // Generate new Object for DB
            ProductHeadGroups newproductheadgroup = new ProductHeadGroups();
            newproductheadgroup.ID = Guid.NewGuid();
            newproductheadgroup.Groupname = txNewGroup.Text;

            // Add to DB
            diveDatabase.AddToProductHeadGroups(newproductheadgroup);
            diveDatabase.SaveChanges();

            // Reload List
            LoadData();
            LoadData2();
        }

        private void HeadGroupList_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            // Save Data on Rowchange
            //bindingSource.EndEdit();
            //diveDatabase.SaveChanges();
        }

       

        private void ProductGroups_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Save Data on FormClose
            //bindingSource.EndEdit();
            //diveDatabase.SaveChanges();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void NewSubGroup_Click(object sender, EventArgs e)
        {
            Guid id_guid = new Guid(txID.Text);
            
            // Generate new Object for DB
            ProductSubGroups newproductsubgroups = new ProductSubGroups();
            newproductsubgroups.ID = Guid.NewGuid();
            newproductsubgroups.Head_Group = id_guid;
            newproductsubgroups.Sub_Group = txNewSubGroup.Text;


            // Add to DB
            diveDatabase.AddToProductSubGroups(newproductsubgroups);
            diveDatabase.SaveChanges();

            // Reload List
            LoadData();
            LoadData2();
        }

        private void HeadGroupList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            LoadData2();
        }
    }
}
